<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\service;

class Redis{
    protected $redis;

    /**
     * Redis constructor.
     */
    public function __construct(){
        $this->redis = new \Redis();
        $bool = $this->redis->connect('127.0.0.1',6379);
        if(!$bool){
            throw new \LogicException('redis链接失败',1006);
        }
    }

    /**
     * 添加redis值
     * @param string $key
     * @param int $time
     * @param string $value
     * @return bool
     */
    public function set($key='',$time=3600,$value=''){
        if(!$key)
            throw new \LogicException('set redis key参数错误',1007);
        if(!$value)
            throw  new \LogicException('set redis value参数错误',1008);
        return $this->redis->setex($key,$time,$value);
    }

    /**
     * 获取redis值
     * @param string $key
     * @return bool|string
     */
    public function get($key=''){
        if(!$key){
            throw new \LogicException('redis key参数错误',1009);
        }
        return $this->redis->get($key);
    }

    /**
     * 删除redis值
     * @param $key
     * @return int
     */
    public function del($key){
        if(!$key){
            throw new \LogicException('redis key参数错误',1009);
        }
        return $this->redis->del($key);
    }

    /**
     * 入队
     * @param $key
     * @param $value
     * @return int
     */
    public function inQue($key,$value){
        $len = $this->redis->lPush($key,$value);
        if(!$len){
            throw new \LogicException('入队失败',1021);
        }
        return $len;
    }

    /**出队
     * @param $key
     * @return string
     */
    public function outQue($key){
        $info = $this->redis->lPop($key);
        if(!$info){
            throw new \LogicException('出队失败',1022);
        }
        return $info;
    }
}